// by Xeno
#define THIS_FILE "fn_sidetanks.sqf"
#include "x_setup.sqf"
private ["_posi_array","_tarray","_dirs","_tank_count","_tank1"];
if !(call FUNC(checkSHC)) exitWith {};

PARAMS_2(_posi_array,_dirs);

GVAR(dead_tanks) = 0;

#ifdef __TT__
GVAR(sm_points_blufor) = 0;
GVAR(sm_points_opfor) = 0;
#endif

_tarray = [];
_tank_count = 3;
for "_ii" from 1 to _tank_count do {
	_tank1 = createVehicle [GVAR(sm_tank), _posi_array select _ii, [], 0, "NONE"];
	_tank1 setDir (_dirs select 0);
	_tank1 setPos (_posi_array select _ii);
	GVAR(x_sm_vec_rem_ar) set [count GVAR(x_sm_vec_rem_ar), _tank1];
	_tank1 addEventHandler ["killed", {
		__INC(GVAR(dead_tanks));
	#ifdef __TT__
		_this call FUNC(AddSMPoints)
	#endif
	}];
	if (GVAR(domdatabase)) then {
		_tank1 addMPEventHandler ["MPKilled", {if (isServer && {isPlayer (_this select 1)}) then {(_this select 1) call FUNC(PAddTankKillPoints)}}];
	};
	// We double the damages caused to the units, to make them explode more easily :)
	_tank1 addEventHandler ["HandleDamage",{ ((_this select 2) * 3) }];
	_tank1 lock true;
	_tarray set [count _tarray, _tank1];
	sleep 0.512;
};

sleep 2.333;
["specops", 2, "basic", 4, _posi_array select 0,150,true] spawn FUNC(CreateInf);
sleep 2.333;
["shilka", 1, "bmp", 1, "tank", 1, _posi_array select 0,2,250,true] spawn FUNC(CreateArmor);

_dirs = nil;
_posi_array = nil;

{_x lock true} forEach _tarray;

sleep 15.321;

while {GVAR(dead_tanks) < _tank_count} do {
	sleep 5.321;
};

#ifndef __TT__
GVAR(sm_winner) = 2;
#else
if (GVAR(sm_points_blufor) > GVAR(sm_points_opfor)) then {
	GVAR(sm_winner) = 2;
} else {
	if (GVAR(sm_points_opfor) > GVAR(sm_points_blufor)) then {
		GVAR(sm_winner) = 1;
	} else {
		if (GVAR(sm_points_opfor) == GVAR(sm_points_blufor)) then {
			GVAR(sm_winner) = 123;
		};
	};
};
#endif
GVAR(sm_resolved) = true;
if (GVAR(IS_HC_CLIENT)) then {
	[QGVAR(sm_var), GVAR(sm_winner)] call FUNC(NetCallEventCTS);
};